package com.tehui.fragment.impl;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.melnykov.fab.FloatingActionButton;
import com.melnykov.fab.ScrollDirectionListener;
import com.tehui.R;
import com.tehui.activity.impl.CommodityActivity;
import com.tehui.activity.impl.MessageActivity;
import com.tehui.activity.impl.MoodsActivity;
import com.tehui.activity.impl.SearchClassifyActivity;
import com.tehui.adapter.HomeAdapter;
import com.tehui.adapter.HomeGridViewAdapter;
import com.tehui.adapter.RollViewPagerAdapter;
import com.tehui.bean.BrandData;
import com.tehui.fragment.BaseFragment;
import com.tehui.global.Global;
import com.tehui.utils.ui.UiUtils;
import com.tehui.utils.web.Update;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by jasmine on 2017/2/28.
 */

public class HomeFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.rlHome)
    RecyclerView       rl;
    @BindView(R.id.llNet)
    LinearLayout       ll;
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipe;
    @BindView(R.id.tvNetError)
    TextView           tvNetError;

    View      line1;
    View      line2;
    ImageView rDesc;
    ImageView rAesc;
    View      line3;
    ImageView jDesc;
    ImageView jAesc;
    View      line4;
    ImageView xDesc;
    ImageView xAesc;

    private boolean                    isRenqi;
    private boolean                    isJiage;
    private boolean                    isXiaoiang;
    private View                       view;
    private View                       headerView;
    private Dialog                     dialog;
    private boolean                    isStart;
    private String                     nomalUrl;
    private String                     nomalSort;
    private Timer                      timer;
    private int                        total_num;//服务器端一共多少条数据
    private List<BrandData.ResultBean> result;
    private HomeAdapter                homeAdapter;
    private boolean                    isLoadmore;
    private ImageView                  img1;
    private ImageView                  img2;
    private ImageView                  img3;
    private TextView                   textView5;
    private TextView                   textView7;
    private TextView                   textView8;
    private GridView                   homeGv;
    private RollPagerView              mRollViewPager;
    private boolean                    isRefresh;
    private FloatingActionButton       fab;
    private GridLayoutManager          gridLayoutManager;

    private static final int PAGE_SIZE = 10;
    private              int page      = 2;

    @Override
    public View initView() {
        view = View.inflate(UiUtils.getContext(), R.layout.home_fragment, null);
        headerView = View.inflate(UiUtils.getContext(), R.layout.home_header_view, null);
        findHeaderView();
        return view;
    }

    private void findHeaderView() {
        homeGv = (GridView) headerView.findViewById(R.id.homeGv);
        //人气榜
        LinearLayout moods = (LinearLayout) headerView.findViewById(R.id.moods);
        moods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpActivity(MoodsActivity.class);
            }
        });
        img1 = (ImageView) headerView.findViewById(R.id.img1);
        img2 = (ImageView) headerView.findViewById(R.id.img2);
        img3 = (ImageView) headerView.findViewById(R.id.img3);
        textView5 = (TextView) headerView.findViewById(R.id.textView5);
        textView7 = (TextView) headerView.findViewById(R.id.textView7);
        textView8 = (TextView) headerView.findViewById(R.id.textView8);
        fab = (FloatingActionButton) view.findViewById(R.id.fab);
        fab.hide(false);
    }

    //加载数据
    @Override
    public void initData() {
        super.initData();
        initSwipe();
        //请求网络
        getDataFromServer(1, Global.URL_DEFAULT, "");
        getUpdate();
        initHomeGridView();
        initRollPagerView();
        getHorizontaData();
        initFab();
        initHomeRg();
    }

    //返回最顶
    private void initFab() {
        fab.attachToRecyclerView(rl, new ScrollDirectionListener() {
            @Override
            public void onScrollDown() {
                fab.hide();
            }

            @Override
            public void onScrollUp() {
                fab.show();
            }

        }, new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                RecyclerView.LayoutManager layoutManager = rl.getLayoutManager();
                if (layoutManager instanceof LinearLayoutManager) {
                    GridLayoutManager linearManager = (GridLayoutManager) layoutManager;
                    int firstVisibleItemPosition = linearManager.findFirstVisibleItemPosition();
                    if (firstVisibleItemPosition > 5) {
                        fab.show();
                    } else {
                        fab.hide();
                    }
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
    }

    private void initSwipe() {
        swipe.setRefreshing(true);
        swipe.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 2;
                homeAdapter.setEnableLoadMore(false);
                //轮播条
                initRollPagerView();
                isLoadmore = false;
                getDataFromServer(1, Global.URL_DEFAULT, "");
                getHorizontaData();
                isRefresh = true;
            }
        });
    }

    //人气榜
    public void getHorizontaData() {
        OkGo.get(Global.URL_MOODS + 3).tag(this).cacheKey("cacheKey").cacheMode(CacheMode.DEFAULT).execute
                (new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                //解析
                if (s.equals("-1")) {
                } else {
                    parseHorizontalJson(s);
                }
            }
        });
    }

    //解析人气榜数据
    private void parseHorizontalJson(String s) {
        BrandData brandData = JSON.parseObject(s, BrandData.class);
        List<BrandData.ResultBean> result = brandData.result;
        textView5.setText(result.get(0).D_title);
        textView7.setText(result.get(1).D_title);
        textView8.setText(result.get(2).D_title);
        setCircularImage(result.get(0).Pic, img1);
        setCircularImage(result.get(1).Pic, img2);
        setCircularImage(result.get(2).Pic, img3);
    }

    //人气榜圆形图片
    private void setCircularImage(String url, ImageView view) {
        Glide.with(UiUtils.getContext()).asBitmap().load(url.trim()).into(new BitmapImageViewTarget(view) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(UiUtils
                        .getContext().getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                view.setImageDrawable(circularBitmapDrawable);
            }
        });
    }

    private void initHomeGridView() {
        homeGv.setAdapter(new HomeGridViewAdapter());
        homeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int content = 0;
                switch (position) {
                    case 0:
                        content = 1;
                        break;
                    case 1:
                        content = 9;
                        break;
                    case 2:
                        content = 10;
                        break;
                    case 3:
                        content = 4;
                        break;
                    case 4:
                        content = 8;
                        break;
                    case 5:
                        content = 6;
                        break;
                    case 6:
                        content = 2;
                        break;
                    case 7:
                        content = 5;
                        break;
                    case 8:
                        content = 3;
                        break;
                    case 9:
                        content = 7;
                        break;
                }
                Intent intent = new Intent();
                intent.putExtra("content", content);
                intent.putExtra("type", 1);
                intent.setClass(UiUtils.getContext(), SearchClassifyActivity.class);
                startActivity(intent);
            }
        });
    }

    //初始化轮播条
    private void initRollPagerView() {
        OkGo.get("http://dl.21tehui.com/AdImgApi.html?type=2").tag(this).cacheKey("cacheKey").cacheMode
                (CacheMode.DEFAULT).execute(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (s != null) {
                    ArrayList<String> imageUrl = new ArrayList<>();
                    ArrayList<String> link = new ArrayList<>();
                    try {
                        JSONArray jsonArray = new JSONArray(s);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            imageUrl.add(new JSONObject(jsonArray.get(i).toString()).get("image").toString());
                            link.add(new JSONObject(jsonArray.get(i).toString()).get("link").toString());
                        }
                        initRollPagerData(imageUrl, link);
                    } catch (JSONException e) {

                    }
                }
            }
        });
    }

    private void initRollPagerData(ArrayList<String> imageUrl, final ArrayList<String> links) {
        //        //轮播条目
        mRollViewPager = (RollPagerView) headerView.findViewById(R.id.rollPagerView);
        mRollViewPager.setPlayDelay(3000);
        //设置透明度
        mRollViewPager.setAnimationDurtion(500);
        //设置指示器
        mRollViewPager.setHintView(new ColorPointHintView(UiUtils.getContext(), Color.RED, Color.GRAY));
        mRollViewPager.setHintPadding(0, 20, 0, 0);

        mRollViewPager.setAdapter(new RollViewPagerAdapter(imageUrl));
        mRollViewPager.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                String s = links.get(position);
                Intent intent = new Intent();
                intent.putExtra("goodsId", s);
                intent.putExtra("isRollView", true);
                intent.putExtra("isFromFavorite", true);
                intent.setClass(UiUtils.getContext(), CommodityActivity.class);
                startActivity(intent);
            }
        });
    }

    //请求网络
    public void getDataFromServer(int page, String url, String sort) {
        if (!UiUtils.isNetworkAvailable()) {
            rl.setVisibility(View.GONE);
            ll.setVisibility(View.VISIBLE);
            swipe.setRefreshing(false);
            return;
        } else {
            ll.setVisibility(View.GONE);
            rl.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(url) && TextUtils.isEmpty(sort)) {

        } else {
            nomalUrl = url;
            nomalSort = sort;
        }

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                sendTimeOutMsg();
            }
        }, Global.TIME_LIMIT);

        OkGo.get(nomalUrl + nomalSort + "&page=" + page).tag(this).cacheKey("cacheKey").cacheMode(CacheMode
                .DEFAULT).execute(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                System.out.println(response + "");
                //解析
                if (s.equals("-1")) {
                    tvNetError.setText("服务器数据异常!");
                    ll.setVisibility(View.VISIBLE);
                    rl.setVisibility(View.GONE);
                    swipe.setRefreshing(false);
                    timer.cancel();
                } else {
                    parseJson(s);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                tvNetError.setText("服务器数据异常!");
                ll.setVisibility(View.VISIBLE);
                rl.setVisibility(View.GONE);
                swipe.setRefreshing(false);
                timer.cancel();
            }
        });
    }

    //解析
    private void parseJson(String s) {
        timer.cancel();
        BrandData brandData = JSON.parseObject(s, BrandData.class);
        total_num = brandData.data.goods_num;
        result = brandData.result;

        System.out.println(isLoadmore + "---" + isRefresh);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipe.setRefreshing(false);
                if (isLoadmore) {
                    homeAdapter.addData(result);
                    homeAdapter.loadMoreComplete();
                    swipe.setEnabled(true);
                } else {
                    if (isRefresh) {
                        homeAdapter.setNewData(result);
                        isRefresh = false;
                        homeAdapter.setEnableLoadMore(true);
                    } else {
                        initRecyclerView();
                    }
                }
            }
        }, 800);
    }

    //网络超时处理
    private void sendTimeOutMsg() {
        OkGo.getInstance().cancelTag(this);
        UiUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                rl.setVisibility(View.GONE);
                ll.setVisibility(View.VISIBLE);
                tvNetError.setText("网络连接超时!");
                swipe.setRefreshing(false);
            }
        });
    }

    //加载rl;
    private void initRecyclerView() {
        homeAdapter = new HomeAdapter(result);
        homeAdapter.setOnLoadMoreListener(this, rl);
        homeAdapter.addHeaderView(headerView);
        gridLayoutManager = new GridLayoutManager(activity, 2);
        rl.setLayoutManager(gridLayoutManager);
        rl.setAdapter(homeAdapter);
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BrandData.ResultBean o = (BrandData.ResultBean) adapter.getData().get(position);
                Intent intent = new Intent(UiUtils.getContext(), CommodityActivity.class);
                intent.putExtra("text", o.D_title);//标题
                intent.putExtra("id", o.GoodsID);//商品id
                intent.putExtra("url", o.discountLink);//优惠卷连接
                intent.putExtra("pic", o.Pic);//图
                intent.putExtra("quan", o.Quan_receive);//已领券
                intent.putExtra("num", o.Sales_num);//购买数
                intent.putExtra("time", o.Quan_time);//过期时间
                intent.putExtra("quanPrice", o.Quan_price);//优惠卷金额
                intent.putExtra("orgPrice", o.Org_Price);//原价
                intent.putExtra("price", o.Price + "");//券后价
                intent.putExtra("cid", o.Cid + "");
                intent.putExtra("IsTmall", o.IsTmall);
                intent.putExtra("Commission", o.Commission_jihua);
                //优惠券id
                intent.putExtra("quanid", o.Quan_id);
                startActivity(intent);
            }
        });
    }

    //点击事件
    @Override
    public void initListener() {
        super.initListener();
    }

    @OnClick({R.id.infoTop, R.id.card_view, R.id.btnNet, R.id.fab})
    public void onClick(View view) {
        page = 2;
        switch (view.getId()) {
            case R.id.infoTop:
                jumpActivity(MessageActivity.class);
                break;
            case R.id.card_view:
                showSearchDialog();
                break;
            case R.id.btnNet:
                initRollPagerView();
                swipe.setRefreshing(true);
                getDataFromServer(1, Global.URL_DEFAULT, "");
                getHorizontaData();
                break;
            case R.id.fab:
                if (gridLayoutManager != null) {
                    UiUtils.MoveToPosition(gridLayoutManager);
                    fab.hide();
                }
                break;
        }
    }

    private void initHomeRg() {
        line1 = headerView.findViewById(R.id.line1);
        line2 = headerView.findViewById(R.id.line2);
        line3 = headerView.findViewById(R.id.line3);
        line4 = headerView.findViewById(R.id.line4);
        rDesc = (ImageView) headerView.findViewById(R.id.rDesc);
        rAesc = (ImageView) headerView.findViewById(R.id.rAesc);
        jDesc = (ImageView) headerView.findViewById(R.id.jDesc);
        jAesc = (ImageView) headerView.findViewById(R.id.jAesc);
        xDesc = (ImageView) headerView.findViewById(R.id.xDesc);
        xAesc = (ImageView) headerView.findViewById(R.id.xAesc);

        headerView.findViewById(R.id.rlZonghe).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //                gridLayoutManager.scrollToPositionWithOffset(0, 0);
                swipe.setRefreshing(true);
                page = 2;
                isRefresh = true;
                isRenqi = false;
                isXiaoiang = false;
                isJiage = false;
                isLoadmore = false;
                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
                rDesc.setVisibility(View.INVISIBLE);
                rAesc.setVisibility(View.INVISIBLE);
                jDesc.setVisibility(View.INVISIBLE);
                jAesc.setVisibility(View.INVISIBLE);
                xDesc.setVisibility(View.INVISIBLE);
                xAesc.setVisibility(View.INVISIBLE);
                getDataFromServer(1, Global.URL_DEFAULT, "");

            }
        });
        headerView.findViewById(R.id.rlRenqi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //                gridLayoutManager.scrollToPositionWithOffset(0, 0);
                swipe.setRefreshing(true);
                page = 2;
                isRefresh = true;
                isXiaoiang = false;
                isJiage = false;
                isLoadmore = false;
                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
                if (isRenqi) {
                    //降序
                    rAesc.setVisibility(View.VISIBLE);
                    rDesc.setVisibility(View.INVISIBLE);
                    isRenqi = false;
                    getDataFromServer(1, Global.URL_QUAN, "ASC");
                } else {
                    //升序
                    rDesc.setVisibility(View.VISIBLE);
                    rAesc.setVisibility(View.INVISIBLE);
                    isRenqi = true;
                    getDataFromServer(1, Global.URL_QUAN, "DESC");
                }
                jDesc.setVisibility(View.INVISIBLE);
                jAesc.setVisibility(View.INVISIBLE);
                xDesc.setVisibility(View.INVISIBLE);
                xAesc.setVisibility(View.INVISIBLE);
            }
        });
        headerView.findViewById(R.id.rlJiage).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //                gridLayoutManager.scrollToPositionWithOffset(0, 0);
                swipe.setRefreshing(true);
                page = 2;
                isRefresh = true;
                isRenqi = false;
                isXiaoiang = false;
                isLoadmore = false;
                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.VISIBLE);
                if (isJiage) {
                    jDesc.setVisibility(View.VISIBLE);
                    jAesc.setVisibility(View.INVISIBLE);
                    isJiage = false;
                    getDataFromServer(1, Global.URL_PRICE, "DESC");
                } else {
                    jAesc.setVisibility(View.VISIBLE);
                    jDesc.setVisibility(View.INVISIBLE);
                    isJiage = true;
                    getDataFromServer(1, Global.URL_PRICE, "ASC");
                }
                rDesc.setVisibility(View.INVISIBLE);
                rAesc.setVisibility(View.INVISIBLE);
                xDesc.setVisibility(View.INVISIBLE);
                xAesc.setVisibility(View.INVISIBLE);
            }
        });
        headerView.findViewById(R.id.rlXiaoliang).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //                gridLayoutManager.scrollToPositionWithOffset(0, 0);
                swipe.setRefreshing(true);
                page = 2;
                isRefresh = true;
                isRenqi = false;
                isJiage = false;
                isLoadmore = false;
                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.VISIBLE);
                if (isXiaoiang) {
                    xAesc.setVisibility(View.VISIBLE);
                    xDesc.setVisibility(View.INVISIBLE);
                    isXiaoiang = false;
                    getDataFromServer(1, Global.URL_SALES_NUM, "ASC");
                } else {
                    xDesc.setVisibility(View.VISIBLE);
                    xAesc.setVisibility(View.INVISIBLE);
                    isXiaoiang = true;
                    getDataFromServer(1, Global.URL_SALES_NUM, "DESC");
                }
                rDesc.setVisibility(View.INVISIBLE);
                rAesc.setVisibility(View.INVISIBLE);
                jDesc.setVisibility(View.INVISIBLE);
                jAesc.setVisibility(View.INVISIBLE);
            }
        });
    }

    //获取升级信息
    public void getUpdate() {
        Update mUpdate = new Update(activity);
        mUpdate.getServreUpdate(false);
    }

    //搜索框
    private void showSearchDialog() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.view_toolbar_search, null);
        final EditText etSearch = (EditText) view.findViewById(R.id.edt_tool_search);
        RelativeLayout rlFinish = (RelativeLayout) view.findViewById(R.id.rlFinsh);
        final ImageView ivClear = (ImageView) view.findViewById(R.id.ivClear);

        dialog = new Dialog(getActivity(), R.style.NobackDialog);
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams
                .MATCH_PARENT);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
        dialog.show();

        rlFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        ivClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etSearch.setText("");
                ivClear.setVisibility(View.GONE);
            }
        });

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                ivClear.setVisibility(View.VISIBLE);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String body = etSearch.getText().toString().trim();
                if (TextUtils.isEmpty(body)) {
                    ivClear.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() ==
                        KeyEvent.KEYCODE_ENTER)) {
                    String body = v.getText().toString();
                    if (TextUtils.isEmpty(body)) {
                        UiUtils.toast("请输入商品名称");
                    } else if (!isStart) {
                        Intent intent = new Intent();
                        intent.putExtra("body", body);
                        intent.putExtra("type", 2);
                        intent.setClass(getActivity(), SearchClassifyActivity.class);
                        startActivity(intent);
                        dialog.dismiss();
                        isStart = true;
                    }
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        isStart = false;
    }

    //加载更多
    @Override
    public void onLoadMoreRequested() {
        swipe.setEnabled(false);
        if (homeAdapter.getData().size() < PAGE_SIZE) {
            homeAdapter.loadMoreEnd(true);
            swipe.setEnabled(true);
        } else {
            if (homeAdapter.getData().size() >= total_num) {
                homeAdapter.loadMoreEnd();
                swipe.setEnabled(true);
            } else {
                isLoadmore = true;
                getDataFromServer(page, "", "");
                page++;
            }
        }
    }
}
